using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Xml.Linq;
namespace MicroMsg.UI.UserContrl
{
	public class ChatMapItem : BaseChatElement
	{
		private const string TAG = "ChatMapItem";
		public ChatMapInfo mInfo;
		public static DependencyProperty MapInfoProperty = DependencyProperty.Register("MapInfo", typeof(string), typeof(ChatMapItem), new PropertyMetadata("", new PropertyChangedCallback(ChatMapItem.OnMapInfoPropertyChanged)));
		public string MapInfo
		{
			get
			{
				return (string)base.GetValue(ChatMapItem.MapInfoProperty);
			}
			set
			{
				base.SetValue(ChatMapItem.MapInfoProperty, value);
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ChatContentCtrl.add_Click(new RoutedEventHandler(this.ChatContentCtrl_Click));
		}
		private void ChatContentCtrl_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		public void RefreshUI(ChatMapInfo mapinfo)
		{
			if (mapinfo == null)
			{
				return;
			}
			this.mInfo = mapinfo;
			if (string.IsNullOrEmpty(mapinfo.label))
			{
				this.GetData(this.mInfo);
			}
			this.MapInfo = mapinfo.label;
		}
		private void GetData(ChatMapInfo mapinfo)
		{
			Log.smethod_5("ChatMapItem", "Begin GetPoiData");
			string text = mapinfo.double_0 + "," + mapinfo.double_1;
			string text2 = "&language=zh-CN&sensor=false";
			string text3 = "&language=en&sensor=false";
			string text4 = "&language=zh-TW&sensor=false";
			string text5;
			if (R.isChineseCN())
			{
				text5 = text2;
			}
			else
			{
				if (R.isChineseTW())
				{
					text5 = text4;
				}
				else
				{
					text5 = text3;
				}
			}
			HttpWebRequest _request = (HttpWebRequest)WebRequest.Create("http://maps.google.com/maps/api/geocode/xml?latlng=" + text + text5);
			EventCenter.registerEventHandler(EventConst.ON_GETADDRESS_SUCCESS, new EventHandlerDelegate(this.onGetAddressSuccess));
			_request.BeginGetResponse(delegate(IAsyncResult asyncResult)
			{
				try
				{
					HttpWebResponse httpWebResponse = (HttpWebResponse)_request.EndGetResponse(asyncResult);
					XDocument xDocument = XDocument.Load(httpWebResponse.GetResponseStream());
					XElement xElement = xDocument.Element("GeocodeResponse");
					if (xElement != null)
					{
						Log.smethod_4("ChatMapItem", "GeocodeResponse is a xml");
						XElement xElement2 = xElement.Element("status");
						if (xElement2 != null)
						{
							string value = xElement2.get_Value();
							if (value.Equals("OK"))
							{
								Log.smethod_4("ChatMapItem", "request position info get right response!");
								using (IEnumerator<XElement> enumerator = xElement.Elements().GetEnumerator())
								{
									while (enumerator.MoveNext())
									{
										XElement current = enumerator.get_Current();
										XElement xElement3 = current.Element("type");
										if (xElement3 != null)
										{
											XElement xElement4 = current.Element("formatted_address");
											if (xElement4 != null)
											{
												this.mInfo.label = xElement4.get_Value();
												Log.smethod_5("ChatMapItem", "strAddress = " + this.mInfo.label);
												EventCenter.postEvent(EventConst.ON_GETADDRESS_SUCCESS, null, null);
												return;
											}
										}
									}
								}
							}
						}
					}
					this.mInfo.label = "";
					EventCenter.postEvent(EventConst.ON_GETADDRESS_SUCCESS, null, null);
				}
				catch (Exception ex)
				{
					this.mInfo.label = "";
					EventCenter.postEvent(EventConst.ON_GETADDRESS_SUCCESS, null, null);
					Log.smethod_1("ChatMapItem", ex.ToString());
				}
			}, null);
			Log.smethod_5("ChatMapItem", "begin get location info");
		}
		public static bool parseMapMsgXML(string xmlStr, ChatMapInfo mapinfo)
		{
			if (!string.IsNullOrEmpty(xmlStr) && mapinfo != null)
			{
				bool result;
				try
				{
					XDocument xDocument = XDocument.Parse(xmlStr);
					XElement xElement = xDocument.Element("msg").Element("location");
					XAttribute xAttribute = xElement.Attribute("x");
					if (xAttribute != null)
					{
						mapinfo.double_0 = double.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("y");
					if (xAttribute != null)
					{
						mapinfo.double_1 = double.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("scale");
					if (xAttribute != null)
					{
						mapinfo.scale = double.Parse(xAttribute.get_Value());
					}
					xAttribute = xElement.Attribute("label");
					if (xAttribute != null)
					{
						mapinfo.label = xAttribute.get_Value();
					}
					xAttribute = xElement.Attribute("maptype");
					if (xAttribute != null)
					{
						mapinfo.maptype = xAttribute.get_Value();
					}
					return true;
				}
				catch (Exception ex)
				{
					Log.smethod_1("ChatMapItem", ex.get_Message());
					result = false;
				}
				return result;
			}
			Log.smethod_5("ChatMapItem", "parseMapMsgXML, input invalid para");
			return false;
		}
		public static bool toMapMsgXML(ChatMapInfo mapinfo, out string xmlStr)
		{
			StringWriter stringWriter = new StringWriter();
			XElement xElement = new XElement("msg");
			bool result;
			try
			{
				XElement xElement2 = new XElement("location");
				xElement2.Add(new XAttribute("x", mapinfo.double_0));
				xElement2.Add(new XAttribute("y", mapinfo.double_1));
				xElement2.Add(new XAttribute("scale", mapinfo.scale));
				xElement2.Add(new XAttribute("label", mapinfo.label));
				xElement2.Add(new XAttribute("maptype", mapinfo.maptype));
				xElement.Add(xElement2);
				xElement.Save(stringWriter);
				goto IL_EC;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ChatMapItem", ex.get_Message());
				xmlStr = "";
				result = false;
			}
			return result;
			IL_EC:
			xmlStr = stringWriter.GetStringBuilder().ToString();
			return true;
		}
		public void onGetAddressSuccess(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_GETADDRESS_SUCCESS)
			{
				EventCenter.removeEventHandler(EventConst.ON_GETADDRESS_SUCCESS, new EventHandlerDelegate(this.onGetAddressSuccess));
				if (!string.IsNullOrEmpty(this.mInfo.label))
				{
					this.MapInfo = this.mInfo.label;
					string text;
					if (ChatMapItem.toMapMsgXML(this.mInfo, out text))
					{
						if (ContactHelper.isChatRoom(this.chatMsg_0.strTalker))
						{
							string text2 = "";
							string text3 = "";
							if (ChatMsgMgr.getMsgUser(this.chatMsg_0, ref text2, ref text3))
							{
								this.chatMsg_0.strContent = text2 + ":\n" + text;
							}
						}
						else
						{
							this.chatMsg_0.strContent = text;
						}
						StorageMgr.chatMsg.modifyMsg(this.chatMsg_0);
					}
				}
			}
		}
		private static void OnMapInfoPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ChatMapItem.MapInfoProperty)
			{
				(dependencyObject_0 as ChatMapItem).OnMapInfoPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnMapInfoPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshUI(dependencyPropertyChangedEventArgs_0.get_NewValue() as ChatMapInfo);
		}
	}
}
